var express = require('express');
var router = express.Router();
var con =require('dao/dbConnect');
var crypto = require('crypto');
var stringUntil = require('../util/stringUntil');

var users = {
  'chenzeen': {
    name: 'Carbo',
    website: 'http://www.byvoid.com'
  }
};

/**
 * 登录验证
 */
router.all('/*', function(req, res, next) {
  var url = req.originalUrl;
  if(req.session.user){
    next();
  } else if(url.indexOf("login")!=-1){
    next();
  } else if(url==="/"){
    next();
  } else if(url.indexOf("toRegiste")!=-1){
    next();
  } else if(url.indexOf("registe")!=-1) {
    next();
  } else {
    res.render('user/login');
  }
});

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render  ('index', { title:  'world'});
});

/**
 * 跳转到登录
 */
router.get('/login', function(req, res, next) {
  res.render('user/login');
});

/**
 * 登录
 */
router.get('/loginUser', function(req, res, next) {
  var userEmail = req.query.userEmail;
  var password = req.query.password;
  client = con.connect();
  client.query("select id,user_email userEmail, password password, user_name userName from base_user_info where user_email = ?",
  [userEmail],
  function(err, results, fields){
    if(results.length==0){
      var resultObj = new Object();
      resultObj.code = "2";
      resultObj.desc = "不存在该email";
      res.send(resultObj);
      return;
    } else {
      var passwordMd5 = crypto.createHash('md5').update(password).digest('hex');
      if(results[0].password == passwordMd5){
        req.session.user = results[0];
        var resultObj = new Object();
        resultObj.code = "0";
        resultObj.desc = "登录成功";
        res.send(resultObj);
        return;
      } else {
        var resultObj = new Object();
        resultObj.code = "2";
        resultObj.desc = "email或者密码不正确";
        res.send(resultObj);
        return;
      }
    }
  });
});

/**
 * 跳转到注册
 */
router.get('/toRegiste', function(req, res, next) {
  res.render('user/registe');
});
 
/**
 * 注册用户
 */
router.get('/registe', function(req, res, next) {
    var userEmail = req.query.userEmail;
    var userName = req.query.userName;
    var password = req.query.password;
    var password1 = req.query.password1;
    if(stringUntil.isNull(userEmail)){
      var resultObj = new Object();
      resultObj.code = "2";
      resultObj.desc = "请填写email";
      res.send(resultObj);
      return;
    }
    if(stringUntil.isNull(userEmail)){
      var resultObj = new Object();
      resultObj.code = "2";
      resultObj.desc = "请填写email";
      res.send(resultObj);
      return;
    }
    if(stringUntil.isNull(userName)){
      var resultObj = new Object();
      resultObj.code = "2";
      resultObj.desc = "请填写用户名";
      res.send(resultObj);
      return;
    }
    if(stringUntil.isNull(password)){
      var resultObj = new Object();
      resultObj.code = "2";
      resultObj.desc = "请填写密码";
      res.send(resultObj);
      return;
    }
    if(stringUntil.isNull(password1)){
      var resultObj = new Object();
      resultObj.code = "2";
      resultObj.desc = "请再次填写密码";
      res.send(resultObj);
      return;
    }
    if(password != password1){
      var resultObj = new Object();
      resultObj.code = "2";
      resultObj.desc = "两次输入密码不一致";
      res.send(resultObj);
      return;
    }
    client = con.connect();
    client.query("select count(*) as count from base_user_info where user_email = ?",
      [userEmail],
      function(err, results, fields){
        if(results[0].count>0){
          var resultObj = new Object();
          resultObj.code = "1";
          resultObj.desc = "存在相同的email";
          res.send(resultObj);
          return;
        } else {
          var passwordMd5 = crypto.createHash('md5').update(password).digest('hex');
          client.query("insert into base_user_info(user_email, password, user_name) values (?,?,?)",
            [userEmail,passwordMd5,userName],
            function (err, results, fields) {
              if(results){
                var resultObj = new Object();
                resultObj.code = "0";
                resultObj.desc = "注册成功";
                res.send(resultObj);
              } else {
                var resultObj = new Object();
                resultObj.code = "1";
                resultObj.desc = "注册失败";
                res.send(resultObj);
              }
            });
        }
      }
    );

});

router.all('/hello/:username', function(req, res,next){
  if(users[req.params.username]){
    next();
  } else {
    next(new Error(req.params.username +"不存在的"));
  }
});

router.get('/hello/:username', function(req, res){
  res.send(JSON.stringify(users[req.params.username]));
});

router.put('/hello/:username', function(req, res){
  console.log("username    put   方法");
  res.send("put 方法");
});

module.exports = router;
